home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: How are multidimensional array stored in memory?
- Date: 1 Mar 1996 12:20:10 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4h7m5qINNqbp@anvil.ugrad.cs.ubc.ca>
- References: <4h6tk6$1d2@mn5.swip.net>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4h6tk6$1d2@mn5.swip.net>,
- Chris Rossall <chris.rossall@mailbox.swipnet.se> wrote:
- >Hello I was just wondering how multidimensional arrays would be stored
- >in memory.For example info[20][3].
- >would infor[0][1] follow info[0][0] or would info[1][0] follow it?
- >Perhaps this is compiler dependant?
-
- It is not compiler dependent. The least significant portion of the address
- depends on the very last index. Thus, the elements of info are stored in memory
- in the order:
-
- info[0][0]
- info[0][1]
- info[0][2]
- info[1][0]
- .
- .
- .
- info[19][2]
-
-
- This is easy to remember, since the least significant index is on the right
- side. Another way to remember is that for incomplete array types, only the
- leftmost index can be []. E.g.
-
- char *twodim[][3] = {
- { "Joe", "Smith", "programmer" },
- { "Mary", "Jones", "engineer" },
- /* more triplets */
- };
-
- Of course, the inner braces can be elided.
-
- In this case, twodim is an array of unspecified size (determined by the number
- of initializers) of little three-membered arrays of char pointers.
-
- Only one [] is ever allowed, must be the leftmost index, and can be present by
- itself.
- --
-
-